qui(
    function (require, exports, module) {
        VOD.NewsHighlight = (function () {
            var lastOption = null;
            var defaults = {
                st: 0,
                et: 0,
                pic: 0,
                color: '',
                bgColor: '',
                removeColor: ''
            };

            function setHighLight(options) {
                if (lastOption != null) {
                    lastOption.removeColor = true;
                    addBackgroundColor(lastOption);
                    $('.p-l-' + lastOption.id).remove();
                    $('.p-r-' + lastOption.id).remove();
                    $('.p-middle-' + lastOption.id).remove();
                }
                if (options != null) {
                    lastOption = $.extend({}, defaults, options);
                    addBackgroundColor(lastOption)
                    addInOutMark(lastOption);
                }else{
                    lastOption = null;
                }
            }


            function addInOutMark(options) {
                var bigMark = '';
                if ($("#changeBigPic").hasClass('btn-primary'))
                    bigMark = 'in-out-mark-b';

                var str = '<div class="absolute in-out-mark-s ' + bigMark + ' p-l p-l-' + options.id + '">' + options.id + '<br>入点</div>';
                $('#img-' + options.st).after(str);
                str = '<div class="absolute in-out-mark-s ' + bigMark + ' p-r p-r-' + options.id + '">' + options.id + '<br>出点</div>';
                $('#img-' + options.et).after(str);

                str = '<div class="absolute in-out-mark-s ' + bigMark + ' p-middle p-middle-' + options.id + '">' + options.id + '<br>封面</div>';
                $('#img-' + options.pic).after(str);
            }

            function addBackgroundColor(options) {
                for (var i = options.st; i <= options.et; i++) {
                    if (!options.removeColor) {
                        $('#img-' + i).addClass(options.color).addClass(options.bgColor);
                    } else {
                        $('#img-' + i).removeClass(options.color).removeClass(options.bgColor);
                    }
                }
            }

            function validCheck() {
                if (lastOption != null) {
                    var findData = VOD.Config.PageBeanNews.findData("regId", parseInt(lastOption.id));
                    if (findData != null) {
                        return parseInt($(".red").parent().find(".pid").html());
                    }
                }
                return null;
            }

            function setNewStart(value) {
                var newstart = value == null ? validCheck() : value;
                var maxValue = parseInt($('#picNum').text())  - 1;

                if (newstart != null) {
                    if (newstart >= maxValue) {
                        console.log("wrong  st " + newstart)
                        return;
                    }
                    if (newstart != lastOption.st) {
                        var newOptions = $.extend(true, {}, lastOption, { st: newstart});
                        var updateObj = {id: lastOption.id, sTime: newstart};
                        if (newstart > lastOption.pic) {
                            updateObj.pic = newstart;
                            newOptions.pic = newstart;
                        }
                        if (newstart >= lastOption.et) {
                            updateObj.eTime = newstart + 1;
                            newOptions.et = newstart + 1;
                        }
                        VOD.AccurateFrame.updateAccurateFrame({head: '', headSetted: false})
                        VOD.Util.updateGridNews(updateObj)
                        VOD.Util.updateTab3Form(updateObj)
                        setHighLight(newOptions);
                    }
                }
            }

            function setNewEnd(value) {
                var newEnd = value == null ? validCheck() : value;
                if (newEnd != null) {
                    if (newEnd == 0) {
                        console.log("wrong et " + newEnd);
                        return;
                    }
                    if (newEnd != lastOption.et) {
                        var newOptions = $.extend(true, {}, lastOption, { et: newEnd});

                        var updateObj = {id: lastOption.id, eTime: newEnd};
                        if (newEnd < lastOption.pic) {
                            updateObj.pic = newEnd;
                            newOptions.pic = newEnd;
                        }
                        if (newEnd <= lastOption.st) {
                            updateObj.sTime = newEnd - 1;
                            newOptions.st = newEnd - 1;
                        }
                        VOD.AccurateFrame.updateAccurateFrame({end: '', endSetted: false})
                        VOD.Util.updateGridNews(updateObj)
                        VOD.Util.updateTab3Form(updateObj)
                        setHighLight(newOptions);
                    }
                }
            }


            function setNewImage(value) {
                var image = value == null ? validCheck() : value;
                if (image != null) {
                    if (image != lastOption.pic) {
                        var newOptions = $.extend(true, {}, lastOption, { pic: image});
                        var updateObj = {id: lastOption.id, pic: image};
                        if (lastOption.st > image) {
                            updateObj.sTime = image;
                            newOptions.st = image;
                            VOD.AccurateFrame.updateAccurateFrame({head: '', headSetted: false})
                        } else if (lastOption.et < image) {
                            updateObj.eTime = image;
                            newOptions.et = image;
                            VOD.AccurateFrame.updateAccurateFrame({end: '', endSetted: false})
                        }
                        VOD.Util.updateGridNews(updateObj)
                        VOD.Util.updateTab3Form(updateObj)
                        setHighLight(newOptions);
                    }
                }
            }

            function getLastOption() {
                if (lastOption == null) {
                    return null;
                }
                return $.extend({}, lastOption);
            }

            function setNewOption(options) {
                switch (options.key) {
                    case "st":
                        setNewStart(options.value);
                        break;
                    case "et":
                        setNewEnd(options.value);
                        break;
                    case "pic":
                        setNewImage(options.value)
                        break;
                }
            }


            return {
                setHighLight: setHighLight,
                setNewStart: setNewStart,
                setNewEnd: setNewEnd,
                setNewImage: setNewImage,
                getLastOption: getLastOption,
                setNewOption: setNewOption
            }
        })()
    }
);